import 'package:dio/dio.dart';

abstract class SDioConfig {

  BaseOptions getBaseOptions() {
    return BaseOptions(
      connectTimeout: const Duration(seconds: 30),
      receiveTimeout: const Duration(seconds: 30),
      contentType: 'application/json;charset=UTF-8',
    );
  }

  // 提取公共response
  Future<dynamic> onResponse(dynamic data, Map extra) async {
    return data;
  }

  // 处理http状态码而非业务码
  void onError(DioException error, Map? extra) {
    throw SDioException(500, error.toString());
  }

}

class SDioException implements Exception {
  final int code;
  final String message;

  SDioException(this.code, this.message);

  @override
  String toString()
  => "DioException > code: ${code.toString()} || message: $message";
}